/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package manejadores;

import entidades.Supervisor;
import interfaz.Entidad;
import interfaz.Manejador;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import persistencia.Conexion;

/**
 *
 * @author Eugenio
 */
public class ManejadorDeSupervisor implements Manejador {

    private ArrayList<Supervisor> supervisores;
    private PreparedStatement ps;
    private ResultSet rs;
    private Conexion con;
    private static ManejadorDeSupervisor instancia;

    public ManejadorDeSupervisor(){
        con=Conexion.getInstancia();
    }

    public static ManejadorDeSupervisor getInstancia(){
        if(instancia==null){
            instancia=new ManejadorDeSupervisor();
        }

        return instancia;
    }


    public int agregar(Entidad supervisor) {
       
        int fila =0;
        String sql = "INSERT INTO supervisor (id_supervisor,nombre,apellido,cedula,sexo,telefono,celular,zona_a_cargo)"+
                     "VALUES(0,?,?,?,?,?,?,?);";
        ps=(PreparedStatement)con.hacerEnunciado(sql);
        try {
            ps.setString(1, ((Supervisor) (supervisor)).getNombre());
            ps.setString(2, ((Supervisor)(supervisor)).getApellido());
            ps.setString(3,((Supervisor)(supervisor)).getCedula());
            ps.setString(4,((Supervisor)(supervisor)).getSexo());
            ps.setString(5,((Supervisor)(supervisor)).getTelefono());
            ps.setString(6,((Supervisor)(supervisor)).getCelular());
            ps.setString(7,((Supervisor)(supervisor)).getZonaCargo());
            fila=ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(ManejadorDeSupervisor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return fila;
    }

    public int eliminar(int id) {
        int fila =0;
        String sql="DELETE FROM supervisor WHERE id_supervisor = ? ;";
        ps=(PreparedStatement)con.hacerEnunciado(sql);
        try {
            ps.setInt(1, id);
            fila=ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ManejadorDeSupervisor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fila;
    }

    public int modificar(Entidad supervisor, int id) {

        int fila =0;
        String sql="UPDATE supervisor SET nombre = ?, apellido = ?, cedula = ?, sexo = ?, telefono = ?,"+
	"celular = ?, zona_a_cargo = ? WHERE id_supervisor = ? ;";
        ps=(PreparedStatement)con.hacerEnunciado(sql);
         try {
            ps.setString(1, ((Supervisor) (supervisor)).getNombre());
            ps.setString(2, ((Supervisor)(supervisor)).getApellido());
            ps.setString(3,((Supervisor)(supervisor)).getCedula());
            ps.setString(4,((Supervisor)(supervisor)).getSexo());
            ps.setString(5,((Supervisor)(supervisor)).getTelefono());
            ps.setString(6,((Supervisor)(supervisor)).getCelular());
            ps.setString(7,((Supervisor)(supervisor)).getZonaCargo());
            ps.setInt(8, id);
            fila=ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(ManejadorDeSupervisor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return fila;
    }

    public ArrayList<Supervisor> getSupervisores(){
        String sql ="SELECT * FROM supervisor";
        supervisores=new ArrayList<Supervisor>();
        ps=(PreparedStatement)con.hacerEnunciado(sql);
        rs=con.hacerConsulta(sql);
        try {
            while (rs.next()) {
                Supervisor supervisor = new Supervisor(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8));
                supervisores.add(supervisor);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ManejadorDeSupervisor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return supervisores;
    }

   
    public int getId(String nombre,String apellido){
       int id=0;
        String sql ="SELECT id_supervisor FROM supervisor WHERE nombre= '"+nombre+"' AND apellido = '"+apellido+"'";
        

        ps=(PreparedStatement)con.hacerEnunciado(sql);
        rs=con.hacerConsulta(sql);
        try {
            while (rs.next()) {
                id=Integer.parseInt (rs.getString(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ManejadorDeSupervisor.class.getName()).log(Level.SEVERE, null, ex);
        }

       
      
       return id;
    }
}
